const { query } = require("./mysql");
const {
	YD_datetimeToString,
	YD_whereSql,
	YD_setSql,
} = require("lib/utils/common");

/** @name 加入黑名单 **/
exports.yd_add = (options) => {
	let sql = `insert into ch_black_list set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 查询黑名单-number **/
exports.yd_find = (options) => {
	const sql = `select
						*
						from ch_black_list
						${YD_whereSql([{ field: "number", type: "全等", value: options.number }])}
	`;
	return query(sql);
};

/** @name 查询所有黑名单列表 **/
exports.yd_listAll = () => {
	const sql = `select
						number
						from ch_black_list
            `;
	return query(sql);
};

/** @name 黑名单列表 **/
exports.yd_list = (options) => {
	const sql = `select
						yb.id, yb.number, yb.note, yb.userId,
						yu.realname, yu.phone,
						${YD_datetimeToString("yb.createTime")},
						${YD_datetimeToString("yb.updateTime")}
						from ch_black_list yb
						left join ch_user yu on yb.userId=yu.uuid
						${YD_whereSql([
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
							{ field: "status", type: "全等", value: options.status },
						])}
						order by yb.id desc
						limit ${(options.pageNum - 1) * options.pageSize},${options.pageSize}
            `;
	return query(sql);
};

/** @name 黑名单总量 **/
exports.yd_count = (options) => {
	const sql = `select count(*) as count
						from ch_black_list yb
						left join ch_user yu on yb.userId=yu.uuid
						${YD_whereSql([
							{ field: "number", type: "模糊", value: options.number },
							{ field: "realname", type: "模糊", value: options.realname },
							{ field: "phone", type: "模糊", value: options.phone },
						])}
            `;
	return query(sql);
};

/** @name 导出黑名单列表 **/
exports.yd_export = () => {
	const sql = `select
						yb.number, yb.note,
						yu.realname, yu.phone
						from ch_black_list yb
						left join ch_user yu on yb.userId=yu.uuid
						order by yb.id desc
            `;
	return query(sql);
};

/** @name 编辑黑名单 **/
exports.yd_update = (options, condition) => {
	let sql = `update ch_black_list set ${YD_setSql(options)} where id=${
		condition.id
	}`;
	return query(sql);
};

/** @name 删除黑名单 **/
exports.yd_delete = (id) => {
	let sql = `delete from ch_black_list where id=${id}`;
	return query(sql);
};
